package com.ray.auth.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ray.auth.system.emun.OrgType;
import com.ray.auth.system.service.ButtonService;
import com.ray.auth.system.service.OrganizationService;
import com.ray.auth.system.table.entity.Button;
import com.ray.auth.system.table.entity.Organization;
import com.ray.common.util.DataGridUtil;
import com.xiaoleilu.hutool.util.ObjectUtil;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.web.controller.WebController;

/**
 * <h1>租户管理 控制器 </h1>
 * @创建人 ray
 * @创建时间 2017-06-12
 * @版本 1.0
 * @重要修改备注 
 * 
 */
@Controller
@RequestMapping("/system/tenant")
public class TenantController extends WebController{
	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private ButtonService buttonService;
	//@Autowired
	//private DictService dictService;
	/**
	 * 租户页面跳转
	 * @return
	 */
	@RequestMapping(value = "view")
	@RequiresPermissions("tenant:view")
	public ModelAndView view(String menuId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //获取
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.setViewName("system/tenant/view");
	  return modelAndView;
	}
	
	/**
	 * 新增页面
	 * @return
	 */
	@RequestMapping(value = "tenantAddView")
	@RequiresPermissions("tenant:add")
	public ModelAndView orgAddView(String menuId,String parentOrgName,Organization record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("parentOrgName", parentOrgName);
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/tenant/add");
	  return modelAndView;
	}
	
	/**
	 * 租户新增
	 * @return
	 */
	@RequestMapping(value = "tenantAdd")
	@RequiresPermissions("tenant:add")
	@ResponseBody
	public ResponseEntity<Result<Object>> add(Organization params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = organizationService.addOrganization(params,OrgType.TENANT.getType(),loginUser);
		return doResult(result);
	}
	
	/**
	 * 租户修改
	 * @return
	 */
	@RequestMapping(value = "tenantUpdate")
	@RequiresPermissions("tenant:edit")
	@ResponseBody
	public ResponseEntity<Result<Object>> edit(Organization params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = organizationService.updateOrganization(params,OrgType.TENANT.getType(),loginUser);
		return doResult(result);
	}
	
	/**
	 * 组织修改页面
	 * @return
	 */
	@RequestMapping(value = "tenantUpdateView")
	@RequiresPermissions("tenant:edit")
	public ModelAndView typeUpdateView(String menuId,String parentTypeId,String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //查询菜单
	  Organization record = organizationService.queryOrg(id,user);
	  
	  Organization parentOrg = organizationService.queryOrg(record.getParentOrgId(), user);
	  if(ObjectUtil.isNotNull(parentOrg)){
		  modelAndView.addObject("parentOrgName", parentOrg.getOrgName());
	  }
	  //获取字典信息
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/tenant/update");
	  return modelAndView;
	}
	
	/**
	 * 组织修改页面
	 * @return
	 */
	@RequestMapping(value = "tenantDetailView")
	@RequiresPermissions("tenant:detail")
	public ModelAndView tenantDetailView(String menuId,String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //查询菜单
	  Organization record = organizationService.queryOrg(id,user);
	  //获取字典信息
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/tenant/detail");
	  return modelAndView;
	}
	
	/**
	 * 租户删除
	 * @return
	 */
	@RequestMapping(value = "tenantDelete")
	@RequiresPermissions("tenant:delete")
	@ResponseBody
	public ResponseEntity<Result<Object>> delete(String id, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = organizationService.deleteOrganization(id,loginUser);
		return doResult(result);
	}
	/**
	 * 租户页面跳转
	 * @return
	 */
	@RequestMapping(value = "query")
	@RequiresPermissions("tenant:query")
	@ResponseBody
	public ResponseEntity<Map<String,Object>> query(Organization params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		PageDto<Organization> page = this.createJQPage(request);
		page = organizationService.queryTenantList(params, page, loginUser);
		return doSuccess(DataGridUtil.dataConvert(page));
	}
	
	/**
	 * 租户页面跳转
	 * @return
	 */
	@RequestMapping(value = "selectQuery")
	@ResponseBody
	public ResponseEntity<Map<String,Object>> selectQuery(Organization params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		PageDto<Organization> page = this.createJQPage(request);
		page = organizationService.queryTenantList(params, page, loginUser);
		return doSuccess(DataGridUtil.dataConvert(page));
	}
	
	@RequestMapping(value = "selectView")
	public ModelAndView select(String menuId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView) {
		LoginUser user = getLoginUser();
		// 查询页面拥有的资源
		 Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	     modelAndView.addObject("buttons", buttons);
		
		modelAndView.setViewName("system/tenant/select");
		return modelAndView;
	}
}
